import Vue from 'vue'
import App from './App.vue'
// ajax请求
import Axios from 'axios';
// 地图插件
import AMap from 'vue-amap';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import VueRouter from 'vue-router'
import routes from './routers'
// svg组件
import './icons'
// 状态管理
import store from './store/index';
//cookie,检测登录过期
import cookie from 'vue-cookie'

Vue.config.productionTip = false;
Vue.prototype.axios = Axios;
Vue.use(VueRouter);
Vue.use(ElementUI);
Vue.use(AMap);
Vue.use(cookie);


const router =new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
});
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
};
Vue.use(router);

AMap.initAMapApiLoader({
  // 高德的key
  key: 'c23a73bb2c062de16b9158396b2817de&plugin=AMap.Geocoder',
  // 插件集合
  plugin: [
    'AMap.Autocomplete',
    'AMap.PlaceSearch',
    'AMap.Scale',
    'AMap.OverView',
    'AMap.ToolBar',
    'AMap.MapType',
    'AMap.PolyEditor',
    'AMap.CircleEditor',
    'AMap.Geolocation',
    'AMap.CitySearch',
  ],
  v: '1.4.4',
});


//请求发送拦截，把数据发送给后台之前做些什么......
Axios.interceptors.request.use((request) => {

  //这个例子中data是loginName和password
  //let REQUEST_DATA = request.data
  //统一进行qs模块转换
  //request.data = qs.stringify(REQUEST_DATA)
  //再发送给后台
  if (router.history.current.path!=='/'&&router.history.current.path!=='/login'&&!cookie.get('user_id')) {
    router.push('/login');
  }
  return request;

}, function (error) {
  // Do something with request error
  return Promise.reject(error);
});


new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App),
}).$mount('#app');
